package com.ygg.cps.afx.jdbc ;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import  com.ygg.cps.afx.jdbc.intf.ResultSetHandler;


public class BeanHandler implements ResultSetHandler
{
    
    private Class<?> clazz;
    
    public BeanHandler(Class<?> clazz)
    {
        this.clazz = clazz;
    }
    
    @Override
    public Object handler(ResultSet rs)
    {
        try
        {
            if (!rs.next())
            {
                return null;
            }
            Object bean = clazz.newInstance();
            // 得到结果集元数据
            ResultSetMetaData metadata = rs.getMetaData();
            int columnCount = metadata.getColumnCount();// 得到结果集中有几列数据
            for (int i = 0; i < columnCount; i++)
            {
               // String coulmnName = metadata.getColumnName(i + 1);// 得到表中每列的列名
                String  columnLabel = metadata.getColumnLabel(i+1)  ; //得到select id_ as id 中的 as 后的别名
                Object coulmnData = rs.getObject(i + 1);
                Field f = clazz.getDeclaredField(columnLabel);// 反射出类上列名对应的属性
                f.setAccessible(true);
                f.set(bean, coulmnData);
            }
            return bean;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
}
